package com.example.demo.web.rest.controller;

import com.example.demo.client.GatewayClient;
import com.example.demo.client.evt.gateway.KeysEvt;
import com.example.demo.client.vm.gateway.OnlineUserVm;
import com.ffcs.oss.param.out.ServiceResp;
import com.example.demo.util.excel.ExcelCreateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 监控在线用户
 * @Author zhengmf
 * @Date 2019/1/3  9:53
 */
@ApiModel(description ="监控在线用户接口")
@RestController
@RequestMapping(value = "/api/monitor")
public class MonitorOnlineController extends BaseController{
    @Autowired
    private GatewayClient gatewayClient;

    @ApiOperation("获取在线用户信息")
    @PostMapping(value = "/onlineUsers")
    public ServiceResp<Map> onlineUsers() {
        Map<String, Object> map = new HashMap<>();
        //在线用户信息
        List<OnlineUserVm> onlineUsers = gatewayClient.getInfo().getBody();
        //当前在线人数
        int onlineUserCount = onlineUsers.size();
        //用户总数
        Long totalUser = baseQueryClient.totalUser().getBody();
        //在线人数占比
        Double onlineRate = Double.parseDouble(String.valueOf(onlineUserCount))/Double.parseDouble(String.valueOf(totalUser));
        onlineRate = Double.valueOf(String.format("%.4f", onlineRate))*100;
        //本日登录用户数
        Long todayLoginUser = baseQueryClient.todayLoginUser().getBody();
        map.put("todayLoginUser", todayLoginUser);//本日登陆用户数
        map.put("onlineRate", onlineRate);//在线人数占比
        map.put("outlineRate", 100-onlineRate); //离线人数占比
        map.put("onlineUsers", onlineUsers);//在线用户信息
        map.put("onlineUserCount", onlineUserCount);//当前在线人数
        map.put("currentTime",  new Date());//当前时间
        return ServiceResp.getInstance(Map.class).success(map);

    }

    @ApiOperation("强制下线")
    @PostMapping(value = "/forceOffline")
    public ServiceResp<Boolean> forceOffline(@RequestBody @Validated KeysEvt evt) {
        return gatewayClient.offline(evt);
    }

    @ApiOperation("导出在线用户信息")
    @GetMapping(value = "/exportOnlineUser")
    public void exportOnlineUser(HttpServletResponse response, String keys) {
        //已选择的记录
        List<OnlineUserVm> userList = new ArrayList<>();
        String[] key;
        if (StringUtils.isNotBlank(keys)) {
            key = keys.split(",");
        } else {
            return;
        }
        List<OnlineUserVm> onlineUsers = gatewayClient.getInfo().getBody();
        for (String aKey : key) {
            for (OnlineUserVm onlineUser : onlineUsers) {
                if (aKey.equals(onlineUser.getUserName() + "_" + onlineUser.getIp())) {
                    userList.add(onlineUser);
                    break;
                }
            }
        }
        if (userList.size() > 0) {
            try {
                String[] title = {"用户名", "登录IP", "心跳时间"};
                List<String> entityKeys = Arrays.asList("userName","ip","loginTime");
                List<Map<String, Object>> list = ExcelCreateUtil.parseListMap(userList, entityKeys);
                Workbook workbook = ExcelCreateUtil.createWorkBook(list, entityKeys, Arrays.asList(title),"sheet1");
                export(response,workbook,"在线用户信息");
            } catch (Exception e) {
                logger.error("在线用户信息导出异常：" + e.getMessage());
                e.printStackTrace();
            }
        }
    }
}
